// mypage/myPrefer/myPrefer.js
var app=getApp();
var util=require("../../utils/util");
var my=require("../../api/my");

Page({
  /*页面的初始数据*/
  data: {
    width: wx.getSystemInfoSync().windowWidth,
    cur_nav: 0,
    id: '',
    nav_list: [],
    pref_list: [],
    pref_text: ['立减券','满减券','礼品券','','平台券'],
    name: '',
    currentPage: 1,
    pageSize: 10,
    hasMoreDate: true,
    token: '',
    lat: 22.80536,
    lng: 113.29321,
    show_nav: false
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that = this;
    var token=wx.getStorageSync('token');
    wx.getStorage({
      key: 'city',
      success:function(res){
        that.setData({lat:res.data.lat,lng:res.data.lng,token:token})
        // 
        that.get_pref_nav(token)
      },
    })
  },
  // 顶部导航
  choose_nav: function (e) {
    var that=this,cur_nav=that.data.cur_nav,token=that.data.token,nav_list=that.data.nav_list;
    var index=e.currentTarget.dataset.index,type=e.currentTarget.dataset.type;
    var status=index!=2?1:3;
    if(index!=cur_nav){
      that.setData({cur_nav:index,status:status,type:type,name:nav_list[index].name,currentPage:1,pref_list:[],show_nav:false})
      // 数据
      that.get_my_pref_list(status,type,token)
    }
  },
  // 优惠券适用范围
  open_more:function(e){
    var that = this,pref_list=that.data.pref_list,cur_nav=that.data.cur_nav;
    var index=e.currentTarget.dataset.index;
    if(cur_nav==1){
      var courseNames=pref_list[index].courseNames?pref_list[index].courseNames:'';
      // if(courseNames){
      //   wx.showToast({
      //     title: courseNames?'适用课程：'+courseNames:'全平台适用',
      //     icon: 'none',
      //     duration: 3000
      //   })
      // }else{
      //   util.page_show_toast('全平台适用',2000)
      // }
      wx.redirectTo({
        url: '/pages/activities/activities',
      })
    }else if(cur_nav==0){
      var businessName=pref_list[index].businessName?pref_list[index].businessName:'';
      var courseNames=pref_list[index].courseNames?pref_list[index].courseNames:'';
      wx.showToast({
        title: pref_list[index].couponUsingRange==1?'适用'+businessName+'以下课程：'+courseNames:'适用'+businessName+'全部课程',
        icon: 'none',
        duration: 3000
      })
    }
  },
  // 跳转
  nav_coll:function(){
    wx.navigateTo({
      url: '../collCoup/collCoup',
    })
  },
  nav_home:function(e){
    var that=this,cur_nav=that.data.cur_nav;
    if(cur_nav==1){
      // wx.switchTab({
      //   url: '../../pages/home/home',
      // })
      wx.redirectTo({
        url: '/pages/activities/activities',
      })
    }else{
      var id=e.currentTarget.dataset.id;
      id=id.indexOf("#")>-1?id.replace(/\#/g, ""):id;
      wx.redirectTo({
        url: '../../pages/mechDetail/mechDetail?id='+id+'&status=8',
      })
    }
  },
  // 触底
  onReachBottom:function(){
    var that=this,hasMoreData=that.data.hasMoreData,status=that.data.status,type=that.data.type,token=that.data.token;
    if(hasMoreData){
      that.get_my_pref_list(status,type,token)
    }else{
      util.page_show_toast('没有更多了',1500)
    }
  },
  // 获取优惠券分类
  get_pref_nav:function(token){
    var that=this;
    my.get_pref_nav(token).then(res=>{
      if(res.data.code==200){
        var list=res.data.data;
        that.setData({nav_list: list,name:list[0].name})
        that.get_my_pref_list(1,1,token)
      }
    })
  },
  // 获取优惠券列表
  get_my_pref_list:function(status,type,token){
    wx.showLoading({title: '加载中'})
    var that=this,currentPage=that.data.currentPage,pageSize=that.data.pageSize,pref_text=that.data.pref_text,cur_nav=that.data.cur_nav;
    var params={
      "currentPage": currentPage,
      "pageSize": pageSize,
      "status": status,
      "comeFrom": type>0?type:'',
      "lat": that.data.lat,
      "lng": that.data.lng
    }
    my.get_my_pref_list(params,token).then(res=>{
      if(res.data.code==200){
        var list=res.data.data.records;
        for(var i=0;i<list.length;i++){
          list[i].beginUseTime=util.formatTime_year(new Date(list[i].beginUseTime))
          list[i].expireTime=util.formatTime_year(new Date(list[i].expireTime))
          list[i].couponWayName=pref_text[list[i].type]
          if(list[i].comeFrom==1){
            list[i].space=list[i].distance>1000?(list[i].distance/1000).toFixed(2):list[i].distance;
          }
        }
        var pref_list = that.data.pref_list;
        pref_list = util.extend(pref_list, list)
        if (list.length < pageSize) {
          that.setData({
            pref_list: pref_list,
            hasMoreData: false
          })
        } else {
          that.setData({
            pref_list: pref_list,
            hasMoreData: true,
            currentPage: currentPage + 1
          })
        }
        if(list.length==0&&pref_list.length==0){
          that.setData({show_nav:true})
        }
      }
    })
  },
})